﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Akso.Entities;
using Akso.ItemCategories.Dto;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Dynamic;
using Abp.Linq.Extensions;
using Abp.AutoMapper;
using Abp.Authorization;
using Akso.Authorization;
using System.Diagnostics;
using Abp.Extensions;

namespace Akso.ItemCategories
{
    [AbpAuthorize(AppPermissions.Pages_PSSModule_ItemCategories)]
    public class ItemCategoryAppService: AbpZeroTemplateAppServiceBase, IItemCategoryAppService
    {
        private readonly IRepository<ItemCategory, Guid> _itemCategoriesRepository;
        public ItemCategoryAppService(IRepository<ItemCategory, Guid> itemCategoriesRepository)
        {
            _itemCategoriesRepository = itemCategoriesRepository;
        }

        #region 查询数据
        private IQueryable<ItemCategory> getQuery(GetItemCategoriesInput input)
        {
            var query = _itemCategoriesRepository.GetAll()
                .WhereIf(
                    !input.Filter.IsNullOrWhiteSpace(),
                    t =>
                        t.ItemCategoryName.Contains(input.Filter)
                );
            return query;
        }
        public async Task<ListResultDto<ItemCategoryListDto>> GetObjects(GetItemCategoriesInput input)
        {
            var query = getQuery(input);
            var dataCount = await query.CountAsync();
            var data = await query.OrderBy(x=>x.CreationTime).PageBy(input).ToListAsync();

            return new PagedResultDto<ItemCategoryListDto>(
                dataCount,
                data.MapTo<List<ItemCategoryListDto>>()
                );
        }
        #endregion

        #region 新增、编辑对象，带权限检查
        /// <summary>
        /// 新增、编辑对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_PSSModule_ItemCategories_Create, AppPermissions.Pages_PSSModule_ItemCategories_Edit)]
        public async Task<ItemCategory> CreateOrUpdateObject(CreateOrUpdateItemCategoryInput input)
        {
            if (input.ItemCategory.Id.HasValue)
            {
                return await UpdateObject(input);
            }
            else
            {
                return await CreateObject(input);
            }
        }

        /// <summary>
        /// 新增对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_PSSModule_ItemCategories_Create)]
        protected virtual async Task<ItemCategory> CreateObject(CreateOrUpdateItemCategoryInput input)
        {
            var o = input.ItemCategory.MapTo<ItemCategory>();
            if (AbpSession.TenantId != null)
                o.TenantId = (int)AbpSession.TenantId;
            else
                o.TenantId = 1; //不能赋值0，估计框架有要求
            return await _itemCategoriesRepository.InsertAsync(o);
        }
        /// <summary>
        /// 编辑对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_PSSModule_ItemCategories_Edit)]
        protected virtual async Task<ItemCategory> UpdateObject(CreateOrUpdateItemCategoryInput input)
        {
            Debug.Assert(input.ItemCategory.Id != null, "input.Classroom.Id should be set.");

            var o = await _itemCategoriesRepository.GetAsync(input.ItemCategory.Id.Value);
            //Update classroom properties
            input.ItemCategory.MapTo(o);
            return await _itemCategoriesRepository.UpdateAsync(o);
        }
        #endregion

        #region 根据Guid获取数据对象
        /// <summary>
        /// 新增、编辑页面中获取对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_PSSModule_ItemCategories_Create, AppPermissions.Pages_PSSModule_ItemCategories_Edit)]
        public async Task<GetItemCategoryForEditOutput> GetObjectForEdit(NullableIdDto<Guid> input)
        {
            var output = new GetItemCategoryForEditOutput
            {

            };

            if (!input.Id.HasValue)
            {
                //Creating a new object
                output.ItemCategory = new ItemCategoryEditDto();
            }
            else
            {
                //Editing an existing object
                var existingObject = await _itemCategoriesRepository.GetAsync(input.Id.Value);

                output.ItemCategory = existingObject.MapTo<ItemCategoryEditDto>();
            }

            return output;
        }
        #endregion
    }
}
